﻿using QuickChat_Service.Repositories.Database;
using QuickChat_Service.Repositories.Repositories.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace QuickChat_Service.Repositories.Repositories.Fakes
{
    public class FakeTalkerRepository : FakeRepository<Talker>, ITalkerRepository
    {
        List<ChatSession> sessions { get; set; }

        public FakeTalkerRepository()
        {
            this.DbSet.Add(new Talker() { TalkerId = 1, ChatAlias = "Talker1", CurrentRoomId = 2 });
            this.DbSet.Add(new Talker() { TalkerId = 2, ChatAlias = "Talker2", CurrentRoomId = 2 });
            this.DbSet.Add(new Talker() { TalkerId = 3, ChatAlias = "Talker3", CurrentRoomId = 2 });
            this.DbSet.Add(new Talker() { TalkerId = 4, ChatAlias = "Talker4", CurrentRoomId = 1 });
            this.DbSet.Add(new Talker() { TalkerId = 5, ChatAlias = "Talker5", CurrentRoomId = 3 });

            ChatSession session1 = new ChatSession();
            session1.SessionId = 1;
            session1.Talkers.Add(GetById(0));
            session1.Talkers.Add(GetById(1));
            session1.Talkers.Add(GetById(2));

            ChatSession session2 = new ChatSession();
            session2.SessionId = 2;
            session2.Talkers.Add(GetById(3));
            session2.Talkers.Add(GetById(4));

            sessions = new List<ChatSession>();

            //sessions.Add(session1);
            //sessions.Add(session2);
        }

        public void EnterChatRoom(int userId, int chatRoomId)
        {
            DbSet.ElementAt(userId).CurrentRoomId = chatRoomId;
        }

        public void LeaveChatRoom(int userId)
        {
            DbSet.ElementAt(userId).CurrentRoomId = null;
        }

        public void CreateNewTalker(string talkerAlias)
        {
            base.Add(new Talker() { TalkerId = base.GetAll().Count(), ChatAlias = talkerAlias });
        }

        public IQueryable<Talker> GetTalkersByRoom(int roomId)
        {
            return (from talker in DbSet
                    where talker.CurrentRoomId == roomId
                    select talker).AsQueryable();
        }

        public IQueryable<Talker> GetTalkersBySession(int sessionId)
        {
            return sessions.SingleOrDefault(sess => sess.SessionId == sessionId).Talkers.AsQueryable();
        }

        public override void Add(Talker entity)
        {
            this.DbSet.Add(entity);
        }

        public override void Update(Talker entity, int targetId)
        {
            for (int i = 0; i < DbSet.Count(); i++)
            {
                if (DbSet.ElementAt(i).TalkerId == entity.TalkerId)
                {
                    DbSet.ElementAt(i).TalkerId = 0;
                }
            }
        }

        public override void Delete(int id)
        {
            Talker talker = DbSet.SingleOrDefault(t => t.TalkerId == id);
            base.Delete(talker);
        }
    }
}